#!/bin/bash
# indique au système que l'argument qui suit est le programme utilisé pour exécuter ce fichier.

# set static variables
tmpSiteDirSVNName="mdnn_read_only"
svnRepository="https://mdnn.googlecode.com/svn/trunk/"

sudo echo "start deployment, please wait ..."

tmpLcMessages=""

if [ ! -z "$LC_MESSAGES" -a "$LC_MESSAGES" != "" ]; then
    tmpLcMessages=$LC_MESSAGES
fi
export LC_MESSAGES=en_US.UTF-8

version=`svn info "${svnRepository}" |grep '^Revision:' | sed -e 's/^Revision: //'`

export LC_MESSAGES=tmpLcMessages

filename="mmdn-r${version}"

cd ~/Documents/Sites/
if [ -d "${filename}" ]; then
        echo "file directory ${filename} already exists... STOP !"
        exit
fi

if [ -d "${tmpSiteDirSVNName}" ]; then
        echo "file directory ${tmpSiteDirSVNName} already exists... STOP !"
        exit
fi

echo "get ${filename} version"

svn checkout "${svnRepository}" "${tmpSiteDirSVNName}"

touch "${tmpSiteDirSVNName}"/app.log
chmod 777 "${tmpSiteDirSVNName}"/app.log
chmod -R 777 "${tmpSiteDirSVNName}"/base_mmdn/fichiers_excel/
chmod -R 777 "${tmpSiteDirSVNName}"/base_mmdn/cron/


#update deployment script from svn last version
if [ -f "${tmpSiteDirSVNName}/deploy.sh" ]; then
    rm deploy.sh
    cp "${tmpSiteDirSVNName}"/deploy.sh .
fi

mv ${tmpSiteDirSVNName} $filename
ln -s ~/Documents/Sites/"${filename}"/presentation ~/Documents/Sites/"${filename}"/base_mmdn/presentation
sudo rm /var/www
sudo ln -s ~/Documents/Sites/"${filename}"/base_mmdn /var/www

echo "end of deployment rev-${version}"
ls -al /var/www

exit